﻿using System;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using mojoPortal.Net;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;
using mojoPortal.Business.WebHelpers;
using System.Web.UI.WebControls;
namespace ud_gSDMS.project
{
    public partial class Changestatus : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private string _id;
        //private string _idnew;
        private SiteUser siteUser;
        private int currentUserID = 0;

        void Page_Load(object sender, EventArgs e)
        {
            siteUser = SiteUtils.GetCurrentSiteUser();
            currentUserID = siteUser.UserId;
            //if (!UserCanEditPage() || !UserCanViewPage())
            //{
            //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            //}
            if (
                //SiteUser.ConfirmRegistration()
                SiteUtils.GetCurrentSiteUser() == null
                )
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }

            if (!Page.IsPostBack)
            {
                PopulateLabels();
                PopulateControls();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    _id = Request["id"];
                    LoadGridMember(_id);
                    LoadData(_id);
                }
            }
        }
        private void PopulateLabels()
        {

        }
        private void LoadGridMember(string id)
        {
            var data = gSDMS_BL.getMemberList(id);
            grdMember.DataSource = data;
            grdMember.DataBind();
        }

        private void PopulateControls()
        {
            var db = new gSDMS_DataLinq();
            cboWorkStatus.DataSource = gSDMS_BL.ud_gSDMS_PROJECT_STEP_LstnotFinsih();
            cboWorkStatus.DataValueField = "CAT_Project_Step_ID";
            cboWorkStatus.DataTextField = "Name";
            cboWorkStatus.DataBind();
            const bool readOnly = true;
            txtProject_Code.ReadOnly = readOnly;
            txtProject_name.ReadOnly = readOnly;
            cboProject_leader.Enabled = !readOnly;
            txtMan_day.ReadOnly = readOnly;
            datKickoff_Date.ReadOnly = readOnly;
            datEnd_Date.ReadOnly = readOnly;
          
        }

        private static string projectid;
        private void LoadData(string id)
        {
            if (string.IsNullOrEmpty(id)) return;
            var db = new gSDMS_DataLinq();
            cboProject_leader.DataValueField = "UserID";
            cboProject_leader.DataTextField = "LoginName";
            cboProject_leader.DataSource = gSDMS_BL.mp_User_Search(null, null, null, null, null, null, 5.ToString());
            cboProject_leader.DataBind();

            var o = gSDMS_BL.ud_gSDMS_PROJECT_Get(id);
            if (o != null)
            {
                projectid = o.Project_ID.ToString();
                hidId.Value = o.Project_ID.ToString();
                if (o.CreatedOn != null) hidCreatedOn.Value = o.CreatedOn.Value.ToShortDateString();
                txtProject_Code.Text = o.ud_gSDMS_CONTRACT.Contract_Code;
                txtProject_name.Text = o.Name;
                cboProject_leader.SelectedValue = o.Project_Leader.ToString();
                datKickoff_Date.Text = o.Kickoff_Date.Value.Day + "/" + o.Kickoff_Date.Value.Month + "/" + o.Kickoff_Date.Value.Year;
                datEnd_Date.Text = o.End_Date.Value.Day + "/" + o.End_Date.Value.Month + "/" + o.End_Date.Value.Year;
                txtMan_day.Text = o.Resource.ToString();//lay nguon luc thuc te
                //lay tang thai work status hien tai
                cboWorkStatus.SelectedValue = o.CAT_Project_Step_ID.ToString();                
            }
           // LoadGridSurvey();
        }
        #region event

        void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _id = hidId.Value;
                //var userSite = SiteUtils.GetCurrentSiteUser();
                gSDMS_BL.ud_gSDMS_PROJECT_UpdChangStatus(ref _id,cboWorkStatus.SelectedValue,null);
                if (!string.IsNullOrEmpty(hidId.Value))
                {
                    lblMes.Text = "Updated successful : Work status ==> " + cboWorkStatus.SelectedItem.Text;
                    lblMes.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblMes.Text = ex.Message;
                lblMes.Visible = true;
            }
        }
       void grdApprovePlan_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            //grdProjectPlan.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
        }
        #endregion event

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            btnSave.Click += btnSave_Click;          
            SuppressPageMenu();
        }
        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion

        protected void txtContract_Code_TextChanged(object sender, EventArgs e)
        {

        }
    }
}